{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1933b95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "threshold_noise 0:  [106, 72, 71]\n",
      "threshold_noise 0:  [90, 73, 73]\n",
      "threshold_noise 0:  [50, 50, 49]\n"
     ]
    }
   ],
   "source": [
    "from QKDNetwork import QKDNetwork\n",
    "from compare_noise import Compare\n",
    "import concurrent.futures\n",
    "import csv\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "# 第一张图：y轴 - 时隙数量, x轴 - 噪声均值\n",
    "# 根据噪声变路由/定路由 的 T vs 噪声均值（两条线）\n",
    "\n",
    "# 第二张图：y轴 - 时隙数量, x轴 - 阈值（超过阈值之后才变路由）\n",
    "# 根据噪声变路由/定路由 的 T vs 阈值（两条线）\n",
    "\n",
    "\n",
    "# Add:  监控网络中残余的密钥\n",
    "#       监控某个SD对还没满足的密钥数量\n",
    "\n",
    "\n",
    "default_avg_noise = 20\n",
    "default_threshold_noise = 20\n",
    "max_core_num = 96\n",
    "\n",
    "\n",
    "def experiment(avg_noise, threshold_noise):\n",
    "    # 节点个数： 60,  源目对数： 50,  阿尔法值： 0.2\n",
    "    # avg_noise - 噪声的均值\n",
    "    # threshold_noise - 噪声的阈值\n",
    "    num_node = 60\n",
    "    sd_num = 50\n",
    "    alpha = 0.2\n",
    "    q = QKDNetwork(\n",
    "        showTopology=False, num_node=num_node, sd_num=sd_num, alpha=alpha, hete=True\n",
    "    )\n",
    "    c = Compare(q, avg_noise, threshold_noise)\n",
    "    data = c.getData()\n",
    "    return data\n",
    "\n",
    "\n",
    "# 改变噪声的均值\n",
    "def run_experiments_var_noise_avg():\n",
    "    total_results = []\n",
    "    for round_idx in range(20):\n",
    "        round_results = []\n",
    "        avg_noise = round_idx\n",
    "        threshold_noise = default_threshold_noise\n",
    "        with concurrent.futures.ThreadPoolExecutor(\n",
    "            max_workers=max_core_num\n",
    "        ) as executor:\n",
    "            future_list = [\n",
    "                executor.submit(experiment, avg_noise, threshold_noise)\n",
    "                for _ in range(96)\n",
    "            ]\n",
    "            for future in concurrent.futures.as_completed(future_list):\n",
    "                data = future.result()\n",
    "                if data is not None and data[0] < 200 and data[1] < 200:\n",
    "                    round_results.append(data)\n",
    "                    print(f\"avg_noise {avg_noise}: \", data)\n",
    "                else:\n",
    "                    break\n",
    "        print(\n",
    "            {\n",
    "                \"avg_noise\": avg_noise,\n",
    "                \"threshold_noise\": threshold_noise,\n",
    "            }\n",
    "        )\n",
    "        print(np.mean(round_results, axis=0))\n",
    "        if not round_results:\n",
    "            break\n",
    "\n",
    "\n",
    "# 改变噪声的阈值，超过一定的阈值之后才变路由\n",
    "def run_experiments_var_noise_threshold():\n",
    "    total_results = []\n",
    "    for round_idx in range(50):\n",
    "        round_results = []\n",
    "        avg_noise = 10\n",
    "        threshold_noise = round_idx\n",
    "        with concurrent.futures.ThreadPoolExecutor(\n",
    "            max_workers=max_core_num\n",
    "        ) as executor:\n",
    "            future_list = [\n",
    "                executor.submit(experiment, avg_noise, threshold_noise)\n",
    "                for _ in range(96)\n",
    "            ]\n",
    "            for future in concurrent.futures.as_completed(future_list):\n",
    "                data = future.result()\n",
    "                if data is not None and data[0] < 200 and data[1] < 200:\n",
    "                    round_results.append(data)\n",
    "                    print(f\"threshold_noise {threshold_noise}: \", data)\n",
    "                else:\n",
    "                    break\n",
    "        if not round_results:\n",
    "            break\n",
    "\n",
    "\n",
    "def run_single_case():\n",
    "    data = experiment(10, 3)\n",
    "    print(data)\n",
    "\n",
    "\n",
    "def run_single_case_parallel():\n",
    "    for noise_threshold in range(1, 20, 3):\n",
    "        total_num = []\n",
    "        with concurrent.futures.ThreadPoolExecutor(\n",
    "            max_workers=max_core_num\n",
    "        ) as executor:\n",
    "            future_list = [\n",
    "                executor.submit(experiment, 10, noise_threshold) for _ in range(96)\n",
    "            ]\n",
    "            for future in concurrent.futures.as_completed(future_list):\n",
    "                data = future.result()\n",
    "                total_num.append(data)\n",
    "                print(data)\n",
    "        print(\"阈值为\", noise_threshold)\n",
    "        print(\"结果为\", np.mean(total_num, axis=0))\n",
    "\n",
    "\n",
    "# run_single_case_parallel()\n",
    "# run_single_case()\n",
    "\n",
    "# run_experiments_var_noise_avg()\n",
    "run_experiments_var_noise_threshold()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
